package cn.schoolwow.ams.test.module.flowLog.flow.business;

import cn.schoolwow.ams.test.entity.User;
import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.quickflow.flow.HasFlowConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserLoginFlow implements BusinessFlow, HasFlowConfig {
    @Autowired
    private DAO starterDAO;

    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String username = (String) flowContext.checkData("username");
        String password = (String) flowContext.checkData("password");

        //查询50次
        for(int i=0;i<50;i++){
            starterDAO.addRecordString("第"+i+"/50次查询");
            Condition condition = starterDAO.query(User.class)
                    .addQuery("username", username)
                    .addQuery("password", password);
            for(int j=0;j<10;j++){
                condition.addRawQuery("1 = 1");
            }
            long count = condition
                    .execute()
                    .count();
            flowContext.putData("count", count);
            if(count==0){
                throw new IllegalArgumentException("用户名或者密码错误!");
            }
        }
    }

    @Override
    public String name() {
        return "用户登录";
    }

    @Override
    public String flowConfigCategoryName() {
        return "系统";
    }

    @Override
    public void setRequestData(JSONObject requestData) {
        requestData.put("流程配置数据", "123456");
    }
}
